' htmlplugin.bmx

Strict

Import BRL.Basic

Import "../maxtk/maxtk.bmx"

Type THTMLOptions Extends TOptionsPage
	Field	owner:THTMLPlugin

	Field	editcolor:TColorButton
	Field	editfont:TFontButton
	Field	editstyle:TTextStyle
	Field	autoindent:TButton
	Field	tabsize:TNumberButton
		
	Method CreateHTMLOptions:THTMLOptions(plugin:THTMLPlugin)
		SetName "HTML Options"
		owner=plugin
		plugin.editor.hostoptions.AddPage Self
'		parent.AddPage Self
		editcolor=AddColorButton("Edit Color",-1)
		editfont=AddFontButton("Edit Font","Courier",12,0)
		editstyle=AddTextStyle(Self,"Style")'ComboButton("Style",["Normal","Bold","Italic","Bold+Italic"])
		autoindent=AddButton("Automatic Indent",2)
		tabsize=AddNumberButton("Tab Size",4)
		layout
		Return Self
	End Method
End Type

Type THTMLPlugin Extends TPlugin

	Field	editor:TEditor
	Field	options:THTMLOptions
	Field	editmenu:TGadget

	Method Open:Object(path$)
		Return THTMLCode.Open(Self,path)
	End Method
	
	Method Initialize(owner:THost)
		host=owner
		SetName("HTML",["html","htm","css"])
'		SetExtensions(["txt","js"])
'		host.Register Self
		editor=TEditor(host)
		If Not editor RuntimeError "HTMLPlugin requires TEditor Host"
		options=New THTMLOptions.CreateHTMLOptions(Self)
		editmenu=CreateEditMenu()
	End Method

	Function CreateEditMenu:TGadget()
		Local	edit:TGadget
		Const MKEY$=Chr(8)
		edit=CreateMenu("&Edit",0,Null)
		CreateMenu "&Quick Help"+MKEY+"F1",MENUQUICKHELP,edit
		CreateMenu "",0,edit
		CreateMenu "Cu&t"+MKEY+"Ctrl+X",MENUCUT,edit
		CreateMenu "&Copy"+MKEY+"Ctrl+C",MENUCOPY,edit
		CreateMenu "&Paste"+MKEY+"Ctrl+V",MENUPASTE,edit
		CreateMenu "",0,edit
		CreateMenu "Select &All"+MKEY+"Ctrl+A",MENUSELECTALL,edit
		CreateMenu "",0,edit
'		CreateMenu "Block Indent"+MKEY+"Ctrl+TAB",MENUINDENT,edit
'		CreateMenu "Block Outdent"+MKEY+"Shift TAB",MENUOUTDENT,edit
		CreateMenu "Block Indent"+MKEY+"Ctrl+]",MENUINDENT,edit
		CreateMenu "Block Outdent"+MKEY+"Ctrl+[",MENUOUTDENT,edit
		CreateMenu "",0,edit
		CreateMenu "&Find"+MKEY+"Ctrl+F",MENUFIND,edit
		CreateMenu "Find Next"+MKEY+"F3",MENUFINDNEXT,edit
		CreateMenu "&Replace"+MKEY+"Ctrl+H",MENUREPLACE,edit
		CreateMenu "&Goto Line"+MKEY+"Ctrl+G",MENUGOTO,edit
		Return edit
	End Function

End Type

Type THTMLCode Extends TOpenCode
	Field	plugin:THTMLPlugin
	Field	options:THTMLOptions

	Function Open:THTMLCode(plugin:THTMLPlugin,path$)
		Local	code:THTMLCode
		
		code=New THTMLCode

		code.plugin=plugin
		code.host=plugin.host
		code.options=plugin.options
		
		code.active=True
		code.path=path
		code.host.addpanel(code)		
		
		code.textarea=CreateTextArea(0,0,ClientWidth(code.panel),ClientHeight(code.panel),code.panel,0)
		SetTextAreaText code.textarea,"~n"
		SetGadgetLayout code.textarea,1,1,1,1
		code.Refresh
		code.SetDirty False
		
		plugin.host.UpdateProgress "Reading Stream"
		code.ReadSource(path)

		code.UpdateCode
		code.undolist.clear
' cludge first dirty		
		code.filesrc=TextAreaText(code.textarea)
		code.SetDirty False
		Return code
	End Function

	Method OnEvent()
 		Select EventSource()
			Case textarea 
				Select EventID() 
					Case EVENT_KEYCHAR
						If EventData()=13 And plugin.options.autoindent AutoIndent 
						Return
					Case EVENT_MOUSEUP
						If EventData()=2
							PopupWindowMenu host.window,plugin.editmenu
						EndIf
					Case EVENT_GADGETACTION
						UpdateCode
					Case EVENT_GADGETSELECT
						UpdateCursor
				End Select
		End Select
	End Method

	Method SetCode(src$,diff:TDiff=Null)
		Local	i,p,p1,q,r,a,t$,h$,lsrc$,r0,r1,cpos,autocap
		Local	s:TTextStyle
' update dirty flag	
		CheckDirty src
		If src=cleansrc And Not deferpos And Not diff Return	
		cleansrc=src
		cleansrcl=src.ToLower()
	End Method

	Method Refresh()
		Local	rgb:TColorButton
		Local	src$
		SetTextAreaTabs textarea,options.tabsize.value
		SetTextAreaFont textarea,options.editfont.font
		rgb=options.editcolor
		SetTextAreaColor textarea,rgb.red,rgb.green,rgb.blue,True
		rgb=options.editstyle.color
		SetTextAreaColor textarea,rgb.red,rgb.green,rgb.blue,False
		src=cleansrc
		cleansrc=""
		cleansrcl=""
		cursorpos=0
		SetCode(src)
	End Method
	
	Method BuildSource(quick,debug,gui,run)		
		host.help "file://"+path 
	End Method	

End Type


